package guo.myapp.spitter.config;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;

@Configuration
//启用SpringMVC
@EnableWebMvc
//组件扫描
@ComponentScan("guo.myapp.spitter")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver(){
        /*
         * 配置JSP视图解析器
         */
        InternalResourceViewResolver resolver =
                new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

//    @Bean
//    public ViewResolver viewResolver(){
//        TilesViewResolver tilesViewResolver = new TilesViewResolver();
//        tilesViewResolver.setPrefix("/WEB-INF/views/");
//        tilesViewResolver.setSuffix(".jsp");
//        tilesViewResolver.setExposeContextBeansAsAttributes(true);
////        tilesViewResolver.setViewClass(JstlView.class);
//        return tilesViewResolver;
//    }

    /**
     * 配置信息源Bean
     * @return
     */
    @Bean
    public MessageSource messageSource(){
        ResourceBundleMessageSource messageSource =
                new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }

    @Bean
    public MessageSource messageSourceReloadable(){
        ReloadableResourceBundleMessageSource messageSource =
                new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("file:///etc/spitter/messages");
        messageSource.setCacheSeconds(10);
        return messageSource;
    }

//    @Bean
//    public TilesConfigurer tilesConfigurer(){
//        TilesConfigurer tilesConfigurer  =
//                new TilesConfigurer();
//        tilesConfigurer.setDefinitions(new String[]{
//                "/WEB-INF/views/tiles.xml",
//                "/WEB-INF/**/tiles.xml"
//        });
//        通配符配置
//        tilesConfigurer.setDefinitions(new String[]{
//                "WEB-INF/**/tiles"
//        });
//        tilesConfigurer.setCheckRefresh(true);
//        return tilesConfigurer;
//    }



    /**
     * 配置静态资源的处理
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
    }
}
